use std::collections::HashMap;
use regex::*;
use async_std::net::{TcpListener,Shutdown, TcpStream};
#[derive(PartialEq,Eq,Hash)]
pub enum HttpParam{
    ROUTER,
    DATA
}
pub struct Request<'a>{
    data :String,
    params:HashMap<HttpParam,String>,
    reader:&'a TcpStream
}
impl <'a> Request<'a>{
    pub fn new(request:String,stream:&'a TcpStream)->Self{
        let mut request= Request{
            data:request,
            params:HashMap::new(),
            reader:stream
        };
        request.process();
        request
    }
    fn process(&mut self){
        let mut in_data = false;
        for line in self.data.lines(){
            println!("{:?}",line);
            if in_data{
                self.params.insert(HttpParam::DATA,line.to_string());
            } 
            else {
                if(line == ""){
                    in_data = true;
                }
                else{
                    let re = Regex::new(r"GET\s.*\sHTTP/").unwrap();  
                    match re.captures(line){
                        None => {},
                        Some(_) =>{  // GET / HTTP/开头为
                            let begin_pos: usize = line.find("GET").unwrap();
                            let end_pos = line.find("HTTP").unwrap();
                            let router =&line[begin_pos+3..end_pos];
                            let router = router.trim();
                            self.params.insert(HttpParam::ROUTER,router.to_string());
                            //println!("{}",router);
                        }
                    }
                }
            }
        }
    }
    pub fn get(&self,param:HttpParam)->Option<& String>{
        self.params.get(&param)
    }
}
